/**
 * [43] 字符串相乘
 *
 * @format
 * @lc app=leetcode.cn id=43 lang=javascript
 */

// @lc code=start
/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var multiply = function (num1, num2) {
    // 初始化数据
    const nums1 = num1.split("").reverse()
    const nums2 = num2.split("").reverse()
    // 进位信息
    let carry = 0
    // 计数结果，位权信息 weight[0] => nums1
    const [res, weight] = [[], [0, 0]]
    // 双层循环，将两个数的每位做笛卡尔积
    // num2: 2567 7 60 500 2000 7 ...   7 ... 2000
    //          * *  *   *    * *       *        *
    // num1:  906 6  6   6    6 0 ... 900 ...  900
    for (let n1 of nums1) {
        weight[1] = 0
        carry = 0
        // if (n1 == 0) 
        for (let n2 of nums2) {
            // if (n2 == 0)
            let i = weight[0] + weight[1]
            if (i > res.length - 1) res.push(0)
            res[i] += n1 * n2 + carry
            carry = Math.floor(res[i] / 10)
            res[i] %= 10
            weight[1]++
            // console.log(`n1: ${n1}, n2: ${n2}`)
            // console.log(`carry: ${carry}, i: ${i}`)
            // console.log(`res: ${res.join("")}`)
        }
        if (carry > 0) res.push(carry)
        weight[0]++
    }
    res.reverse()
    return res[0] == 0 ? "0" : res.join("")
}
// debugger
// let num1 = "123"
// let num2 = "456"
// let product = multiply(num1, num2)
// console.log(product)
// @lc code=end
